// battle.c
// All battle-related routines.
// Copyright (C)1997 BJ Eirich

#include "battle.h"

FILE *d;

char whotoattack()
{ unsigned char t1,t2;

  if (numchars==1) return partyidx[0]-1;

tryagain:
  t1=rand();
  t2=(255/numchars)+1;
  t1=(t1/t2);
  if (t1==numchars) goto tryagain;
  if (party_stats[(int)partyidx[(int)t1]].status==1) goto tryagain;

  return partyidx[t1]-1;
}

int random_range(int min, int max)
{ int i;

rnd1:
  i=(rand() % (max+1));
  if ((i>=min) && (i<=max)) return i;
  else goto rnd1;
}

int getnum()
{ int i,tmin,tmax;

  fscanf(d,"%d",&tmin);
  fscanf(d,"%d",&tmax);
  i=random_range(tmin,tmax);
  return i;
}

int getsnum()
{ int i,tmin;

  fscanf(d,"%d",&tmin);
  i=tmin;
  return i;
}

void levelup (int chr)
{ int i,i3,j;
  int alreadyhave, inparty;
  unsigned char* img;

  d=fopen("PARTY.DAT","r");
  fscanf(d,"%s",strbuf);
  for (i=0; i<=chr; i++)
      { fscanf(d,"%s",strbuf);
        fscanf(d,"%s",strbuf);
        fscanf(d,"%s",strbuf); }
  fclose(d);

  d=fopen((const char*)strbuf,"r");
  fgets((char*)strbuf,99,d);
  fgets((char*)strbuf,99,d);

  party_stats[chr].lv++;
  for (i=0; i<party_stats[chr].lv; i++)
      fgets((char*)strbuf,99,d);

  fscanf(d,"%d",&party_stats[chr].nxt);
  i=getnum();
  party_stats[chr].maxhp+=i;
  party_stats[chr].curhp+=i;
  i=getnum();
  party_stats[chr].maxmp+=i;
  party_stats[chr].curmp+=i;
  i=getnum();
  party_stats[chr].str+=i;
  i=getnum();
  party_stats[chr].end+=i;
  i=getnum();
  party_stats[chr].mag+=i;
  i=getnum();
  party_stats[chr].mgr+=i;
  i=getnum();
  party_stats[chr].hit+=i;
  i=getnum();
  party_stats[chr].dod+=i;
  i=getnum();
  party_stats[chr].mbl+=i;
  i=getnum();
  party_stats[chr].fer+=i;
  i=getnum();
  party_stats[chr].rea+=i;

  i = getsnum();

  alreadyhave = 0;
  i3 = 0;

  while (i3 <= 24)
  {
    if (i == party_stats[chr].maginv[i3]) alreadyhave = 1;
    i3++;
  }

  if (!alreadyhave)
  {         
     i3 = 0;
     inparty = 0;
     while (i3 < 5)
     {
     if (partyidx[i3] == (chr+1)) inparty = 1;
     i3++;
     }

     if (inparty) {
       img=magicicons+(magic[i].icon*256);
       strcpy((char*)strbuf, party_stats[chr].name);
       strcat((char*)strbuf, " has learned");
      
       while (!b1 && !b2 && !b4)
       {
         drawmap();
         tmenubox(176-(strlen((char*)strbuf)*5), 80, 176+(strlen((char*)strbuf)*4), 118);
         gotoxy(186-(strlen((char*)strbuf)*5), 86);
         printstring((char*)strbuf);
         tcopysprite(160,96,16,16,img);
         vgadump();
         readcontrols();
       }

       drawmap();
       vgadump();
     }

     j=party_stats[chr].magcnt;
     if (j!=24)
       {
         party_stats[chr].maginv[j]=i;
         party_stats[chr].magcnt++;
       }
     else party_stats[chr].maginv[j-1]=i;       
   }
    
  fclose(d);

  UpdateEquipStats();
}

void battle()
{ unsigned char t1;

  t1=whotoattack();

  if (party_stats[t1].curhp<=2)
      party_stats[t1].curhp=0;
  else
  { party_stats[t1].curhp-=2;
    party_stats[t1].exp+=2; }

  gp+=5;
  if (party_stats[t1].curhp<1) party_stats[t1].status=1;
  if (party_stats[t1].nxt<=party_stats[t1].exp) levelup(t1);
}
